﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using MyWeb.Common;using MyWeb.Business;using MyWeb.Data;

namespace MyWeb.Admins{public partial class Customers : System.Web.UI.Page{static bool Insert = false;protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){Session["folder"] = "Customers"; BindGrid();}}
private void BindGrid(){grdCustomers.DataSource = CustomersService.Customers_GetByAll() ;grdCustomers.DataBind();}
protected void btnThem_Click(object sender, EventArgs e){pnShow.Visible = false;pnUpdate.Visible = true;ControlClass.ResetControlValues(this);Session["upload"] = null;Insert=true;}
protected void btnXoa_Click(object sender, EventArgs e){try{DataGridItem item = default(DataGridItem);for (int i = 0; i < grdCustomers.Items.Count; i++){item = grdCustomers.Items[i];if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item){if (((CheckBox)item.FindControl("ChkSelect")).Checked){string strId = item.Cells[1].Text;try{CustomersService.Customers_Delete(strId);}catch{}}}}grdCustomers.CurrentPageIndex = 0;BindGrid();}catch{}}protected void grdCustomers_ItemCommand(object source, DataGridCommandEventArgs e){Data.CustomersInfo strCA=new CustomersInfo();
string id = e.CommandArgument.ToString();
if  (e.CommandName=="Edit"){DataTable dt = CustomersService.Customers_GetById(id);txtId.Text=dt.Rows[0]["Id"].ToString();txtName.Text=dt.Rows[0]["Name"].ToString();txtAddress.Text=dt.Rows[0]["Address"].ToString();txtTel.Text=dt.Rows[0]["Tel"].ToString();txtEmail.Text=dt.Rows[0]["Email"].ToString();txtyear.Text=dt.Rows[0]["year"].ToString();pnUpdate.Visible = true;pnShow.Visible = false;}else{CustomersService.Customers_Delete(id);BindGrid();}}protected void lblBack_Click(object sender, EventArgs e){pnUpdate.Visible = false;pnShow.Visible = true;Insert=false;}protected void lblUpdate_Click(object sender, EventArgs e){string image = Session["upload"] == null ? "" : Session["upload"].ToString();
    #region[TestInput]
if (txtName.Text.Trim().Equals("")){WebMsgBox.Show("Name không thể trống !");txtName.Focus();return;}if (txtAddress.Text.Trim().Equals("")){WebMsgBox.Show("Address không thể trống !");txtAddress.Focus();return;}if (txtTel.Text.Trim().Equals("")){WebMsgBox.Show("Tel không thể trống !");txtTel.Focus();return;}if (txtEmail.Text.Trim().Equals("")){WebMsgBox.Show("Email không thể trống !");txtEmail.Focus();return;}if (txtyear.Text.Trim().Equals("")){WebMsgBox.Show("year không thể trống !");txtyear.Focus();return;}
#endregion
Data.CustomersInfo obj=new Data.CustomersInfo();obj.Id = txtId.Text;obj.Name = txtName.Text;obj.Address = txtAddress.Text;obj.Tel = txtTel.Text;obj.Email = txtEmail.Text;obj.year = txtyear.Text;if (Insert == true){CustomersService.Customers_Insert(obj);}else{CustomersService.Customers_Update(obj);}BindGrid();pnShow.Visible = true;pnUpdate.Visible = false;Insert = false;
}protected void grdCustomers_PageIndexChanged(object source, DataGridPageChangedEventArgs e){grdCustomers.CurrentPageIndex = e.NewPageIndex;BindGrid();} protected void grdCustomers_ItemDataBound(object sender, DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator)){if (itemType == ListItemType.Header){object checkBox = e.Item.FindControl("chkSelectAll");if ((checkBox != null)){((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
}
}else{
string tableRowId = grdCustomers.ClientID + "_row" + e.Item.ItemIndex.ToString();
e.Item.Attributes.Add("id", tableRowId);
object checkBox = e.Item.FindControl("chkSelect");
if ((checkBox != null))
{e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");}}}}
}
}